package com.gs.wzweather.utils;

import com.gs.wzweather.bo.LatestWea;
import com.gs.wzweather.entity.Site;
import com.gs.wzweather.entity.Weather;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class ParseUtil {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    public static List<String> parseStaIds(String staIds) {
        List<String> list = new ArrayList<>();
        try {
            if (staIds != null) {
                String[] idArray = staIds.split(",");
                for (int i = 0; i < idArray.length; i++) {
                    list.add(idArray[i]);
                }
            }
        } catch (Exception e) {
            System.out.print("parseStaIds->" + e.getMessage());
        }
        return list;
    }


    /*
    检查空值的站点
    如果空值站点>10 把空值站点用查询最新资料数据代替
     */
    public static String checkIsNull(List<Site> sites) {
        try {
            String staIds = null;
            StringBuffer sb = new StringBuffer();
            int emptyNum = 0;
            if (sites != null) {
                for (int i = 0; i < sites.size(); i++) {
                    Site site = sites.get(i);
                    if (site.getWeather().getValue() == null) {
                        emptyNum++;
                        sb.append(site.getStationId()).append(",");
                    }
                }
                if (emptyNum > 10) {
                    staIds = sb.substring(0, sb.length() - 2).toString();
                    return staIds;
                }
            }
        } catch (Exception e) {
            // logger.info(("checkIsNull->{}"), e.getMessage());
            return null;
        }
        return null;
    }

    /*
    解析最新资料数据
     */
    public static void parseLatestWea(List<Site> sites, LatestWea latestWea) {
        StringBuffer sb = new StringBuffer();
        List<LatestWea.DSBean> ds = latestWea.getDS();
        try {
            if (latestWea != null) {
                for (int i = 0; i < ds.size(); i++) {
                    getField(sites, ds.get(i));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void getField(List<Site> sites, Object obj) throws Exception {
        Field[] fields = obj.getClass().getDeclaredFields(); //获取实体类的所有属性，返回Field数组
        String staId = null;
        String value = null;
        for (int j = 0; j < fields.length; j++) { //遍历所有属性
            Field field = fields[j];
            field.setAccessible(true);
            if (field.getName().equals("stationId")) {
                staId = (String) field.get(obj);
            } else if (!field.getName().equals("serialVersionUID") && field.get(obj) != null) {
                value = (String) field.get(obj);
            }
        }
        for (int i = 0; i < sites.size(); i++) {
            Site site = sites.get(i);
            if (site.getStationId().equals(staId)) {
                Weather weather = site.getWeather();
                weather.setStationId(staId);
                weather.setValue(value);
                break;
            }
        }
    }

    public static List<Site> formatSites(List<Site> sites) {
        try {
            if (sites != null) {
                for (Site site : sites) {
                    if (site != null) {
                        Weather weather = site.getWeather();
                        if (weather != null) {
                            weather.setValue(saveOne(weather.getValue()));
                        }
                    }
                }
            }
            return sites;
        } catch (Exception e) {
            return sites;
        }
    }

    private static String saveOne(String value) {
        try {
            if (value == null) {
                return value;
            }
            double num = Double.parseDouble(value);
            DecimalFormat df = new DecimalFormat("0.0");
            return df.format(num);
        } catch (Exception e) {
            return value;
        }
    }
}
